<?php
/**
 * Created by PhpStorm.
 * User: xiaotao
 * Date: 18-5-30
 * Time: 下午1:42
 */

namespace app\manage\controller;
use think\Controller;
use think\exception\HttpResponseException;
use think\facade\Cache;
use think\facade\Request;


/**
 * 权限验证基类
 * Class Base
 * @package app\service\controller
 */
abstract class Base extends Controller
{
    protected $token = null;
    /**
     * @var null
     * uname
     * uid
     * group
     * rules
     * routers
     */
    protected $baseInfo = null;

    // 是否必须进行控制器验证，false只进行登录验证
    protected $mustAuth     = true;
    // 是否为只有管理员可访问
    protected $onlyAdmin    = false;

    /**
     * 父类在构造函数中调用，如何提起结束请求
     */
    protected function initialize()
    {
        // 根据token判断是否已经登录
        $token = Request::param('token');
        $this->token = $token;

        // 利用缓存进行权限验证
        if (!$token && strlen($token) !== 32) {
            $result = $this->result_json('缺少Token', 401, null);
            throw new HttpResponseException($result);
        }

        $cache = Cache::get($token);
        if ($cache === false) {
            $result = $this->result_json('用户登录已过期，请重新登录', 401, null);
            throw new HttpResponseException($result);
        }

        // 超级管理员验证
        if ($cache['group'] === 0) {
            // 权限验证成功，继续后续操作
            $this->baseInfo = $cache;
            return;
        }
        if ($this->onlyAdmin === true) {
            $result = $this->result_json('不被允许的请求', 405, null);
            throw new HttpResponseException($result);
        }

        // 其他高级的认证，参阅TP提供的验证类
        // 是否必须进行规则验证
        if ($this->mustAuth === true) {
            // 根据当前的控制器方法进行验证
            $method = Request::controller() . '/' . Request::action();
            $rules  = $cache['rules'];
            unset($cache['rules']);
            unset($cache['routers']);
            if (!in_array($method, $rules)) {
                $result = $this->result_json('不被允许的请求', 405, null);
                throw new HttpResponseException($result);
            }
        }
        // 权限验证成功，继续后续操作
        $this->baseInfo = $cache;
    }

    /**
     * 空方法处理
     * @return string
     */
    public function _empty() {
        return 'api not exist';
    }

    /**
     * 错误返回
     * @param string $message 错误消息
     * @param int $code 错误码
     * @param int $http_code http状态码
     * @return \think\response\Json
     */
    protected function result_json($message = '', $code = 0, $data = null, $http_code = 200) {
        $response = [
            'code'    => $code
        ];
        if ($message) $response['message'] = $message;
        if ($data) $response['data'] = $data;

        return json($response, $http_code);
    }

    /**
     * 将数组转化为Tree结构
     * @param $list
     * @param int $root
     * @param string $root_prop
     * @param string $child_prop
     * @return array
     */
    public static function ListTree($list, $root = 0, $root_prop = 'pid', $child_prop = 'children') {
        // 是否有必要保留为Collection
        $data = [];
        foreach ($list as $item) {
            if ($item[$root_prop] === $root) {
                $item[$child_prop] = self::ListTree($list, $item['id'], $root_prop, $child_prop);
                $data[] = $item;
            }
        }
        return $data;
    }

}